package com.dragon.utilslibrary.network.http;


import android.util.Log;

import androidx.annotation.NonNull;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * 文件上传
 */
public class FileUploader extends HttpBase{
    private static final String TAG = "uploadFile";
    private static final int TIME_OUT = 10*1000; //超时时间
    private static final String CHARSET = "utf-8"; //设置编码
    private static final String PREFIX = "--";
    private static final String LINE_END = "\r\n";
    
    private boolean isCancelled = true;
    
    
    public  void upload(@NonNull HttpRequest request, @NonNull HttpListioner<HttpRespose> listioner){
    	isCancelled = true;
    	String host = request.getUrl();
    	Map<String, File> fileParams = request.getFileParems();
    	Map<String, Object> params = request.getFileStringParens();
    	String Content_Type = request.getContent_Type();
    	HttpMethod method = request.getHttpMethod();//请求方式 GET or POST
    	
    	onStartThread(listioner);
    	
    	HttpRespose respose = new HttpRespose();
        String BOUNDARY = UUID.randomUUID().toString(); //边界标识 随机生成 String PREFIX = "--" , LINE_END = "\r\n";
        String CONTENT_TYPE = "multipart/form-data"; //内容类型
        if(fileParams == null || fileParams.isEmpty()){
        	respose.setErroCode(-1);
        	respose.setMsg("fileParams is null");
        	respose.setE(new NullPointerException("fileParams is null"));
        	onResposeThread(listioner, respose);
//        	return respose;
        }
        Set<String> key = fileParams.keySet();
        String name = null;
        File file = null ;
        for (String s : key) {
        	name = s;
        	file = fileParams.get(name);
		}
        try {
            URL url = new URL(host);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            if(request.getTimeOut() <= 0){
	            conn.setReadTimeout(TIME_OUT);
	            conn.setConnectTimeout(TIME_OUT);
            }else {
            	conn.setReadTimeout(request.getTimeOut());
                conn.setConnectTimeout(request.getTimeOut());
			}
            conn.setRequestMethod(method.name());
			if (method == HttpMethod.POST) {
				conn.setRequestMethod("POST");
				conn.setDoOutput(true); // 发送POST请求必须设置允许输出
				conn.setDoInput(true); // 发送POST请求必须设置允许输入
												// setDoInput的默认值就是true
			}
            conn.setRequestProperty("Charset", CHARSET);//设置编码
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
            conn.setDoInput(true); //允许输入流
            conn.setDoOutput(true); //允许输出流
            conn.setUseCaches(false); //不允许使用缓存
            if(file!=null) {
                /** * 当文件不为空，把文件包装并且上传 */
                OutputStream outputSteam=conn.getOutputStream();
                DataOutputStream dos = new DataOutputStream(outputSteam);
                StringBuffer sb = new StringBuffer();
                sb.append(LINE_END);
                if(params!=null){//根据格式，开始拼接文本参数
                    for(Map.Entry<String, Object> entry:params.entrySet()){
                        sb.append(PREFIX).append(BOUNDARY).append(LINE_END);//分界符
                        sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINE_END);
                        sb.append("Content-Type: text/plain; charset=" + CHARSET + LINE_END);
                        sb.append("Content-Transfer-Encoding: 8bit" + LINE_END);
                        sb.append(LINE_END);
                        sb.append(entry.getValue());
                        sb.append(LINE_END);//换行！
                    }
                }
                sb.append(PREFIX);//开始拼接文件参数
                sb.append(BOUNDARY); sb.append(LINE_END);
                /**
                 * 这里重点注意：
                 * name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件
                 * filename是文件的名字，包含后缀名的 比如:abc.png
                 */
                sb.append("Content-Disposition: form-data; name=\""+name+"\"; filename=\""+file.getName()+"\""+LINE_END);
                sb.append("Content-Type: "+Content_Type+"; charset="+CHARSET+LINE_END);
                sb.append(LINE_END);
                //写入文件数据
                dos.write(sb.toString().getBytes());
                InputStream is = new FileInputStream(file);
                byte[] bytes = new byte[1024];
                long totalbytes = file.length();
                long curbytes = 0;
                Log.i("cky","total="+totalbytes);
                int len = 0;
                while((len=is.read(bytes))!=-1 && isCancelled == true){
                    curbytes += len;
                    dos.write(bytes, 0, len);
                    dos.flush();
                    onProgressThread(listioner, curbytes, totalbytes);
//                    listener.onProgress(curbytes,totalbytes);
                }
                is.close();
                dos.write(LINE_END.getBytes());//一定还有换行
                byte[] end_data = (PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
                dos.write(end_data);
                dos.flush();
                /**
                 * 获取响应码 200=成功
                 * 当响应成功，获取响应的流
                 */
                int code = conn.getResponseCode();
                sb.setLength(0);
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while((line=br.readLine())!=null){
                    sb.append(line);
                }
                if(code == 200){
                	respose.setJson(sb.toString());
                }else {
                	respose.setMsg(sb.toString());
                }
                respose.setErroCode(code);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            respose.setErroCode(-1);
            respose.setE(e);
        } catch (IOException e) {
            e.printStackTrace();
            respose.setErroCode(-1);
            respose.setE(e);
        }
        onResposeThread(listioner, respose);
//    	return respose;
    }
    public void cancelled(){
    	isCancelled = false;
    }
}